package co.liyao.controller;


import co.liyao.model.BizArticle;
import co.liyao.model.BizComment;
import co.liyao.model.BizType;
import co.liyao.model.vo.BizArticleVo;
import co.liyao.service.BizArticleService;
import co.liyao.service.BizArticleTagService;
import co.liyao.service.BizTypeService;
import co.liyao.service.MessageService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class BizArticleController {

    @Autowired
    private BizArticleService service;

    @Autowired
    private BizArticleTagService tagService;

    @Autowired
    private BizTypeService typeService;

    @Autowired
    private MessageService messageService;

    /**
     *  重定向到 index
     * @param vo
     * @param model
     * @return
     */
    @RequestMapping("/")
    public String index(BizArticleVo vo,Model model){

        return "redirect:index";
    }

    /**
     *  /index 頁面
     *
     * @param vo
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String PageIndex(BizArticleVo vo, Model model){
           /**
         * 第一次 进入 默认
         */
        if(vo.getPageNum()==null){
            vo.setPageNum(0);
        }
        if(vo.getTagId()!=null){
            model.addAttribute("tagId",vo.getTagId());
        }
        if(vo.getTypeId()!=null){
            model.addAttribute("typeId",vo.getTypeId());
        }else{
            model.addAttribute("categoryId","index");
        }
         model.addAttribute("page",service.FindBizArticlePage(vo));
         //引用公共部分
        common(model);
        //返回首页 判断是否有数据  愚昧的方法  已经在改造
        PageInfo<BizArticle> info = service.FindBizArticlePage(vo);
        List<BizArticle> list = info.getList();
        if(list.size()==0){
            model.addAttribute("listState",true);
        }else{
            model.addAttribute("listState",false);
        }

        return "BlogWeb/index";

    }

    /**
     * 根據id 查詢 文章
     * @param artId
     * @param model
     * @return
     */
    @RequestMapping("/index/{artId}")
    public String PageIndex(@PathVariable("artId") Integer artId, Model model){
        BizArticle bizArticleById = service.findBizArticleById(artId);
        if(bizArticleById==null){
            // 就返回一个404
            model.addAttribute("head",typeService.findAll());
            return "BlogWeb/404";
        }
        model.addAttribute("art",bizArticleById);
        common(model);
        return "BlogWeb/details";
    }

    @RequestMapping("/message")
    public String message(Model model,Integer pageNum){

        if(pageNum==null){
            pageNum = 0;
        }
        model.addAttribute("page",messageService.findAll(pageNum));
        model.addAttribute("categoryId","message");
        common(model);
        return "BlogWeb/message";
    }



    //公用的部分
    private void common(Model model){
        model.addAttribute("head",typeService.findAll());
        model.addAttribute("tag",tagService.findAll());
        model.addAttribute("time",service.findByBizArticleTime());
        model.addAttribute("look",service.findLook());
        model.addAttribute("rec",service.findRec());
        model.addAttribute("count",messageService.findCount());
        model.addAttribute("likeNum",messageService.findlike());


    }


}




